﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Microsoft.PKS
{
	public class LfsFile
	{
		public string FileName { get; set; }
		public string ServerPath { get; set; }
		public string ClientPath { get; set; }
		public string ClientLink { get; set; }
		public bool IsPrimary { get; set; }
		public bool IsInLocalFileStore { get; set; }

		public string ResolveServerFileName()
		{
			string ret = ServerPath;
			if (!ret.EndsWith("\\") && !ret.EndsWith("/"))
			{
				string slash = "\\";
				if (ret.Contains("/")) { slash = "/"; }
				ret += slash;
			}
			return ret + FileName;
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();
			sb.Append("FileName: ");
			sb.AppendLine(FileName);

			sb.Append("IsPrimary: ");
			sb.AppendLine(IsPrimary.ToString());

			sb.Append("IsInLocalFileStore: ");
			sb.AppendLine(IsInLocalFileStore.ToString());

			sb.Append("ServerPath: ");
			sb.AppendLine(ServerPath);

			sb.Append("ClientPath: ");
			sb.AppendLine(ClientPath);

			sb.Append("ClientLink: ");
			sb.AppendLine(ClientLink);

			return sb.ToString();
		}
	}

	public class LfsFileList : List<LfsFile>
	{
		public bool UpdatePrimaryInformation { get; set; }

		public int PrimaryFileIndex
		{
			get
			{
				for (int i = 0; i < Count; i++)
				{
					if (this[i].IsPrimary) { return i; }
				}

				if (Count > 0) { this[0].IsPrimary = true; return 0; }

				return -1;
			}
		}

		public LfsFile PrimaryFile
		{
			get
			{
				int idx = PrimaryFileIndex;
				if (idx < 0 || idx >= Count) { return null; }
				return this[idx];
			}
		}

		public LfsFileList()
		{ }

		public LfsFileList(string Xml)
		{
			LoadXml(Xml);
		}

		public LfsFileList(XmlDocument Xml)
		{
			LoadXml(Xml);
		}

		public void LoadXml(string Xml)
		{
			Clear();
			if (string.IsNullOrEmpty(Xml)) { return; }
			Xml = Xml.Trim();
			if (string.IsNullOrEmpty(Xml)) { return; }

			XmlDocument d = new XmlDocument();
			d.LoadXml(Xml);
			LoadXml(d);
		}

		public void LoadXml(XmlDocument Xml)
		{
			this.Clear();

			if (Xml == null) { return; }

			foreach (XmlNode xn in Xml.ChildNodes)
			{
				if (string.Equals(xn.Name, "ExternalFilesMetadata", StringComparison.InvariantCultureIgnoreCase))
				{
					foreach (XmlNode xnb in xn.ChildNodes)
					{
						if (string.Equals(xnb.Name, "Files", StringComparison.InvariantCultureIgnoreCase))
						{
							foreach (XmlNode file in xnb.ChildNodes)
							{
								LfsFile f = new LfsFile();

								if (string.Equals(file.Name, "ExternalFileInfo", StringComparison.InvariantCultureIgnoreCase))
								{
									foreach (XmlAttribute xa in file.Attributes)
									{
										if (string.Equals(xa.Name, "IsPrimary", StringComparison.InvariantCultureIgnoreCase))
										{
											bool isPrimary = false;
											bool.TryParse(xa.Value, out isPrimary);
											f.IsPrimary = isPrimary;
										}
										else if (string.Equals(xa.Name, "IsInLocalFileStore", StringComparison.InvariantCultureIgnoreCase))
										{
											bool isInLocalFileStore = false;
											bool.TryParse(xa.Value, out isInLocalFileStore);
											f.IsInLocalFileStore = isInLocalFileStore;
										}
									}
								}

								foreach (XmlNode property in file.ChildNodes)
								{
									if (string.Equals(property.Name, "FileName", StringComparison.InvariantCultureIgnoreCase))
									{
										f.FileName = property.InnerText;
									}
									else if (string.Equals(property.Name, "ServerPath", StringComparison.InvariantCultureIgnoreCase))
									{
										f.ServerPath = property.InnerText;
									}
									else if (string.Equals(property.Name, "ClientPath", StringComparison.InvariantCultureIgnoreCase))
									{
										f.ClientPath = property.InnerText;
									}
									else if (string.Equals(property.Name, "ClientLink", StringComparison.InvariantCultureIgnoreCase))
									{
										f.ClientLink = property.InnerText;
									}
								}

								Add(f);
							}
						}
						else if (string.Equals(xnb.Name, "UpdatePrimaryInformation", StringComparison.InvariantCultureIgnoreCase))
						{
							bool updatePrimaryInformation = false;
							bool.TryParse(xnb.InnerText, out updatePrimaryInformation);
							UpdatePrimaryInformation = updatePrimaryInformation;
						}
					}
				}
			}
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder();

			sb.Append("UpdatePrimaryInformation: ");
			sb.AppendLine(UpdatePrimaryInformation.ToString());
			sb.AppendLine("----------------------------------------------");
			foreach (LfsFile f in this)
			{
				sb.Append(f.ToString());
				sb.AppendLine("----------------------------------------------");
			}
			return sb.ToString();
		}

	}
}